<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="��Ա�ȼ�id" prop="levelId">
      <el-input v-model="dataForm.levelId" placeholder="��Ա�ȼ�id"></el-input>
    </el-form-item>
    <el-form-item label="设备id" prop="deviceId">
      <el-input v-model="dataForm.deviceId" placeholder="设备id"></el-input>
    </el-form-item>
    <el-form-item label="用户名" prop="username">
      <el-input v-model="dataForm.username" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="dataForm.password" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="盐" prop="salt">
      <el-input v-model="dataForm.salt" placeholder="盐"></el-input>
    </el-form-item>
    <el-form-item label="昵称" prop="nickname">
      <el-input v-model="dataForm.nickname" placeholder="昵称"></el-input>
    </el-form-item>
    <el-form-item label="手机号" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="手机号"></el-input>
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="dataForm.email" placeholder="邮箱"></el-input>
    </el-form-item>
    <el-form-item label="头像" prop="header">
      <el-input v-model="dataForm.header" placeholder="头像"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="gender">
      <el-input v-model="dataForm.gender" placeholder="性别"></el-input>
    </el-form-item>
    <el-form-item label="生日" prop="birthday">
      <el-input v-model="dataForm.birthday" placeholder="生日"></el-input>
    </el-form-item>
    <el-form-item label="城市" prop="city">
      <el-input v-model="dataForm.city" placeholder="城市"></el-input>
    </el-form-item>
    <el-form-item label="职业" prop="job">
      <el-input v-model="dataForm.job" placeholder="职业"></el-input>
    </el-form-item>
    <el-form-item label="个性签名" prop="sign">
      <el-input v-model="dataForm.sign" placeholder="个性签名"></el-input>
    </el-form-item>
    <el-form-item label="来源" prop="sourceType">
      <el-input v-model="dataForm.sourceType" placeholder="来源"></el-input>
    </el-form-item>
    <el-form-item label="会员结束时间" prop="endTime">
      <el-input v-model="dataForm.endTime" placeholder="会员结束时间"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="注册时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="注册时间"></el-input>
    </el-form-item>
    <el-form-item label="免费次数" prop="freecount">
      <el-input v-model="dataForm.freecount" placeholder="免费次数"></el-input>
    </el-form-item>
    <el-form-item label="苹果id" prop="appleId">
      <el-input v-model="dataForm.appleId" placeholder="苹果id"></el-input>
    </el-form-item>
    <el-form-item label="微信id" prop="wxId">
      <el-input v-model="dataForm.wxId" placeholder="微信id"></el-input>
    </el-form-item>
    <el-form-item label="vip" prop="vip">
      <el-input v-model="dataForm.vip" placeholder="vip"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          levelId: '',
          deviceId: '',
          username: '',
          password: '',
          salt: '',
          nickname: '',
          phone: '',
          email: '',
          header: '',
          gender: '',
          birthday: '',
          city: '',
          job: '',
          sign: '',
          sourceType: '',
          endTime: '',
          status: '',
          createTime: '',
          freecount: '',
          appleId: '',
          wxId: '',
          vip: ''
        },
        dataRule: {
          levelId: [
            { required: true, message: '��Ա�ȼ�id不能为空', trigger: 'blur' }
          ],
          deviceId: [
            { required: true, message: '设备id不能为空', trigger: 'blur' }
          ],
          username: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],
          salt: [
            { required: true, message: '盐不能为空', trigger: 'blur' }
          ],
          nickname: [
            { required: true, message: '昵称不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '手机号不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '邮箱不能为空', trigger: 'blur' }
          ],
          header: [
            { required: true, message: '头像不能为空', trigger: 'blur' }
          ],
          gender: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          birthday: [
            { required: true, message: '生日不能为空', trigger: 'blur' }
          ],
          city: [
            { required: true, message: '城市不能为空', trigger: 'blur' }
          ],
          job: [
            { required: true, message: '职业不能为空', trigger: 'blur' }
          ],
          sign: [
            { required: true, message: '个性签名不能为空', trigger: 'blur' }
          ],
          sourceType: [
            { required: true, message: '来源不能为空', trigger: 'blur' }
          ],
          endTime: [
            { required: true, message: '会员结束时间不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '注册时间不能为空', trigger: 'blur' }
          ],
          freecount: [
            { required: true, message: '免费次数不能为空', trigger: 'blur' }
          ],
          appleId: [
            { required: true, message: '苹果id不能为空', trigger: 'blur' }
          ],
          wxId: [
            { required: true, message: '微信id不能为空', trigger: 'blur' }
          ],
          vip: [
            { required: true, message: 'vip不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/user/user/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.levelId = data.user.levelId
                this.dataForm.deviceId = data.user.deviceId
                this.dataForm.username = data.user.username
                this.dataForm.password = data.user.password
                this.dataForm.salt = data.user.salt
                this.dataForm.nickname = data.user.nickname
                this.dataForm.phone = data.user.phone
                this.dataForm.email = data.user.email
                this.dataForm.header = data.user.header
                this.dataForm.gender = data.user.gender
                this.dataForm.birthday = data.user.birthday
                this.dataForm.city = data.user.city
                this.dataForm.job = data.user.job
                this.dataForm.sign = data.user.sign
                this.dataForm.sourceType = data.user.sourceType
                this.dataForm.endTime = data.user.endTime
                this.dataForm.status = data.user.status
                this.dataForm.createTime = data.user.createTime
                this.dataForm.freecount = data.user.freecount
                this.dataForm.appleId = data.user.appleId
                this.dataForm.wxId = data.user.wxId
                this.dataForm.vip = data.user.vip
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/user/user/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'levelId': this.dataForm.levelId,
                'deviceId': this.dataForm.deviceId,
                'username': this.dataForm.username,
                'password': this.dataForm.password,
                'salt': this.dataForm.salt,
                'nickname': this.dataForm.nickname,
                'phone': this.dataForm.phone,
                'email': this.dataForm.email,
                'header': this.dataForm.header,
                'gender': this.dataForm.gender,
                'birthday': this.dataForm.birthday,
                'city': this.dataForm.city,
                'job': this.dataForm.job,
                'sign': this.dataForm.sign,
                'sourceType': this.dataForm.sourceType,
                'endTime': this.dataForm.endTime,
                'status': this.dataForm.status,
                'createTime': this.dataForm.createTime,
                'freecount': this.dataForm.freecount,
                'appleId': this.dataForm.appleId,
                'wxId': this.dataForm.wxId,
                'vip': this.dataForm.vip
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
